001 /* EVolve - an Extensible Software Visualization Framework 002 * Copyright (C) 2001-2002 Qin Wang 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Library General Public 006 * License as published by the Free Software Foundation; either 007 * version 2 of the License, or (at your option) any later version. 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Library General Public License for more details. 013 * 014 * You should have received a copy of the GNU Library General Public 015 * License along with this library; if not, write to the 016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 017 * Boston, MA 02111-1307, USA. 018 */ 019 020 /* 021 * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/ 022 */ 023 024 package EVolve.data; 025 026 import EVolve.*; 027 import java.awt.*; 028 import java.util.*; 029 import java.util.regex.*; 030 031 public class Selection implements Cloneable{ 032 private String name; 033 private int sourceType, entityType; 034 private Entity[] selected; 035 private long start, end; 036 private ReferenceLink[] link; 037 private Color color; 038 private static int total = 0; 039 private long startTime = -1, endTime = -1; 040 private ArrayList timeMap; 041 042 public Selection(int sourceType, int entityType, long[] selected, long start, long end, ArrayList timemap) { 043 this.entityType = entityType; 044 this.sourceType = sourceType; 045 this.selected = new Entity[selected.length]; 046 for (int i = 0; i < selected.length; i++) { 047 this.selected[i] = (Entity)Scene.getDataManager().getEntity()[entityType].get(new Long(selected[i])); 048 } 049 this.start = start; 050 this.end = end; 051 this.color = null; 052 this.link = new ReferenceLink[0]; 053 054 this.name = "Selection" + total; 055 total++; 056 timeMap = new ArrayList(); 057 if (timemap != null) { 058 for (int i=0; i<timemap.size(); i++) { 059 long[] newValue = new long[2]; 060 long[] oldValue = (long[])timemap.get(i); 061 newValue[0] = oldValue[0]; 062 newValue[1] = oldValue[1]; 063 timeMap.add(newValue); 064 } 065 findTimeInterval(start, end); 066 } 067 } 068 069 public void setName(String name) { 070 this.name = name; 071 } 072 073 public String getName() { 074 return name; 075 } 076 077 public void setColor(Color color) { 078 this.color = color; 079 } 080 081 public Color getColor() { 082 return color; 083 } 084 085 public void setLink(ReferenceLink[] link) { 086 this.link = link; 087 } 088 089 public ReferenceLink[] getLink() { 090 return link; 091 } 092 093 public int getEntityType() { 094 return entityType; 095 } 096 097 public Entity[] getSelected() { 098 return selected; 099 } 100 101 public void setSelected(Entity[] selected) { 102 this.selected = selected; 103 } 104 105 public long getStart() { 106 return start; 107 } 108 109 public long getEnd() { 110 return end; 111 } 112 113 public long getStartTime() { 114 return startTime; 115 } 116 117 public long getEndTime() { 118 return endTime; 119 } 120 121 public void setStartEvent(int start) { 122 startTime = start; 123 } 124 125 public void setEndEvent(int end) { 126 endTime = end; 127 } 128 129 public void setTimeInterval(long startTime, long endTime) { 130 this.startTime = startTime; 131 this.endTime = endTime; 132 findEventInterval(); 133 } 134 135 public Selection specialClone() { 136 long ids[] = new long[selected.length]; 137 138 for (int i=0; i<ids.length; i++) { 139 ids[i] = selected[i].getId(); 140 } 141 142 //Selection returnVal = new Selection(entityType,ids,0,Integer.MAX_VALUE,-1,-1,null); 143 Selection returnVal = new Selection(sourceType,entityType,ids,0,Long.MAX_VALUE,null); 144 returnVal.setColor(color); 145 returnVal.setName(name); 146 returnVal.setLink(link); 147 148 return returnVal; 149 } 150 151 public ArrayList getTimeMap() { 152 return timeMap; 153 } 154 155 public Object clone() { 156 Selection o = null; 157 158 try { 159 o = (Selection)super.clone(); 160 } catch (CloneNotSupportedException e) { 161 System.out.println("Clone not supported in selection"); 162 return o; 163 } 164 165 o.name = name; 166 o.selected = new Entity[selected.length]; 167 for (int i=0; i<selected.length; i++) { 168 o.selected[i] = selected[i]; 169 } 170 o.link = new ReferenceLink[link.length]; 171 for (int i=0; i<link.length; i++) { 172 o.link[i] = (ReferenceLink)link[i].clone(); 173 } 174 o.color = color == null ? null : new Color(color.getRGB()); 175 return o; 176 } 177 178 private void findTimeInterval(long start, long end) { 179 int found = 0; 180 if (timeMap != null) { 181 for (int i = 0; i< timeMap.size(); i++) { 182 long[] map = (long[])timeMap.get(i); 183 if ((startTime == -1) && (map[1] == start)) { 184 startTime = map[0]; 185 found++; 186 } 187 if (map[1] == end) { 188 endTime = map[0]; 189 found++; 190 } 191 if (found == 2) { 192 if (endTime == -1) endTime = Integer.MAX_VALUE; 193 return; 194 } 195 } 196 } 197 } 198 199 private void findEventInterval() { 200 long time[] = new long[2], event[] = new long[2]; 201 time[0] = startTime; 202 time[1] = endTime; 203 event[0] = 0; 204 event[1] = Long.MAX_VALUE; 205 206 if (timeMap != null) { 207 for (int i=0; i<time.length; i++) { 208 long lastTarget = -1; 209 for (int j = 0; j< timeMap.size(); j++) { 210 long[] map = (long[])timeMap.get(j); 211 if ((lastTarget<= time[i])&&(map[0]>=time[i])) { 212 event[i] = map[1]; 213 break; 214 } 215 } 216 } 217 start = event[0]; 218 end = event[1]; 219 } 220 } 221 222 public void changeTimeFrame(long startTime, long endTime) { 223 setTimeInterval(startTime, endTime); 224 findEventInterval(); 225 } 226 227 public int getSourceType() { 228 return sourceType; 229 } 230 231 public void filterEntities(String regExps, boolean keepMatched) { 232 HashSet result = new HashSet(); 233 234 StringTokenizer token = new StringTokenizer(regExps,";"); 235 while (token.hasMoreTokens()) { 236 String regExp = token.nextToken(); 237 Pattern pattern = Pattern.compile(regExp); 238 Matcher matcher = pattern.matcher(""); 239 240 for (int i=0; i<selected.length; i++) { 241 String entityName = selected[i].getName().replace('.','/'); 242 matcher.reset(entityName); 243 244 if ((matcher.find() == keepMatched)&&(!result.contains(new Integer(i)))) { 245 result.add(new Integer(i)); 246 } 247 } 248 } 249 250 Entity[] newSelected = new Entity[result.size()]; 251 int i = 0; 252 for (int j=0; j<selected.length; j++) { 253 if (result.contains(new Integer(j))) 254 newSelected[i++] = selected[j]; 255 } 256 selected = null; 257 selected = newSelected; 258 } 259 }